package client;

import java.io.Serializable;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Search_Control extends JPanel implements Serializable
{
	public SearchDetails sd = new SearchDetails();
	public int counter = 0;
	public int NumberOfSearchFields = 5;
	public float f = -1;	
	public int i = -1;
	public boolean flag = true;
	public static ArrayList<Item> l = null; 
	public static ArrayList<Item> list = null; 
	public static Object[][] tempData = null;
	public static Object[] imgCounter = null;
	public static Object[] onSpecialSale = null;

	@SuppressWarnings("unchecked")
	public Search_Control(String min, String max, String itemId, String[] colorsSelected, String[] typesSelected, String callfrom)
	{			
		sd.setCallFrom(callfrom);//catalog or search
		
		if(min.isEmpty())
		{
			sd.setminPrice(-1); //put -1 if no value entered
			counter++;
		}
		else
		{	//float from string
			try{f = Float.valueOf(min.trim()).floatValue();}
		    catch (NumberFormatException nfe)
		    {
		    	JOptionPane.showMessageDialog(null,"Only digits are permitted, Reenter min value ","Error",JOptionPane.ERROR_MESSAGE);
		    	flag = false;
		    }
			sd.setminPrice(f);
		}
		if(flag != false)
		{
			if(max.isEmpty())
			{	
				sd.setmaxPrice(-1); //put -1 if no value entered
				counter++;
			}
			else 
			{	//float from string
				try{f = Float.valueOf(max.trim()).floatValue();}
			    catch (NumberFormatException nfe)
			    {
			    	JOptionPane.showMessageDialog(null,"Only digits are permitted, Reenter max value ","Error",JOptionPane.ERROR_MESSAGE);
			    	flag = false;
			    }
			    sd.setmaxPrice(f); 
			}
			
			if(flag != false)
			{
				if(itemId.isEmpty())
				{	
					sd.setitemId(-1); //put -1 if no value entered
					counter++;
				}
				else 
				{
					//Integer from string	
					try{i = Integer.valueOf(itemId.trim()).intValue();}
				    catch (NumberFormatException nfe)
				    {
				    	JOptionPane.showMessageDialog(null,"Only digits are permitted, Reenter item id ","Error",JOptionPane.ERROR_MESSAGE);
				    	flag = false;
				    }
				    sd.setitemId(i); 
				}
				
				if(colorsSelected.length == 0)// no colors chosen 
				{
					sd.setcolorsSelected(null);
					counter++;
				}
				else sd.setcolorsSelected(colorsSelected);
				
				if(typesSelected.length == 0)// no types chosen
				{
					sd.settypesSelected(null);
					counter++;
				}
				else sd.settypesSelected(typesSelected);
				
				if(counter == NumberOfSearchFields)
					JOptionPane.showMessageDialog(null,"No fields was chosen, You must fill at least 1 field","ERROR",JOptionPane.ERROR_MESSAGE);	
			}
		} 		
		if(flag != false)
		{
			ObjHandle.sendToServer(sd);
						
			l = (ArrayList<Item>)Main.clie.getResult();
			list = new ArrayList<Item>(l.size());
			list = l;
				
			tempData = new Object[list.size()][8];
			imgCounter = new Object[list.size()];
			onSpecialSale = new Object[list.size()];
			   
			for (int i=0; i < list.size(); i++)
			{        
			    tempData[i][0] = list.get(i).getName();
			    tempData[i][1] = new ImageIcon(list.get(i).getMyIconArray());
			    tempData[i][5] = list.get(i).getId();
			    tempData[i][3] = list.get(i).getPrice(); 
			    tempData[i][4] = list.get(i).getItemTypNname();
			    tempData[i][2] = "";
			    tempData[i][6] = "";
			    tempData[i][7] = "";
			    imgCounter[i] = new ImageIcon(list.get(i).getMyImageArray());
			    if(list.get(i).discount == 1)
			    	onSpecialSale[i] = 1;
			    else
			    	onSpecialSale[i] = 0;
			}
		}
	}
}

